package com.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.domain.Role;
import com.li.domain.RoleDept;
import com.li.domain.RoleMenu;
import com.li.exception.NotFondException;
import com.li.mapper.RoleDeptMapper;
import com.li.mapper.RoleMapper;
import com.li.mapper.RoleMenuMapper;
import com.li.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleDeptMapper roleDeptMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Transactional
    @Override
    public String addRole(Role role) {
        if (role==null){
            throw new NotFondException("角色不存在");
        }
        //添加角色
        role.setCreateTime(new Date());
        int insert = roleMapper.insert(role);
        return String.valueOf(insert);
    }

    @Transactional
    @Override
    public String updateRole(Role role) {
        if (role==null){
            throw new NotFondException("角色不存在");
        }
        //修改角色
        int insert = roleMapper.updateById(role);
        Integer roleId=role.getRoleId();
        //删除角色部门和角色菜单表对应的roleId
        roleDeptMapper.delete(new QueryWrapper<RoleDept>().eq("role_id",roleId));
        roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("role_id",roleId));
        //添加角色部门
        if (role.getDeptIds()!=null){
            for (Integer deptId:role.getDeptIds()){
                RoleDept roleDept=new RoleDept();
                roleDept.setRoleId(roleId);
                roleDept.setDeptId(deptId);
                roleDeptMapper.insert(roleDept);
            }
        }
        if (role.getMenuIds()!=null){
            //添加角色菜单
            for (Integer menuId:role.getMenuIds()){
                RoleMenu roleMenu=new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                roleMenuMapper.insert(roleMenu);
            }
        }
        return String.valueOf(insert);
    }

    @Transactional
    @Override
    public void delRole(Integer roleId) {
        //删除角色部门和角色菜单表对应的roleId
        roleDeptMapper.delete(new QueryWrapper<RoleDept>().eq("role_id",roleId));
        roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("role_id",roleId));
        roleMapper.deleteById(roleId);
    }

    @Override
    public List<Role> listRole(Role role, Integer page, Integer limit) {
        Integer indexPage=(page-1)*limit;
        return roleMapper.listRole(role,indexPage,limit);
    }

    @Override
    public Integer listRoleCount(Role role) {
        return roleMapper.listRoleCount(role);
    }

    @Override
    public Role editRoleAndDeptIdAndMenuId(Role role) {
        Role r = roleMapper.editRoleAndDeptIdAndMenuId(role.getRoleId());
        if (r.getMenuIdsString()!=null){
            String []menuIdsString=r.getMenuIdsString().split(",");
            Integer []menuIds=new Integer[menuIdsString.length];
            for (int i=0;i<menuIdsString.length;i++){
                menuIds[i]=Integer.valueOf(menuIdsString[i]);
            }
            r.setMenuIds(menuIds);
        }
        if (r.getDeptIdsString()!=null){
            String []deptIdsString=r.getDeptIdsString().split(",");
            Integer []deptIds=new Integer[deptIdsString.length];
            for (int i=0;i<deptIdsString.length;i++){
                deptIds[i]=Integer.valueOf(deptIdsString[i]);
            }
            r.setDeptIds(deptIds);
        }
        return r;
    }
}
